<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-position="left"
      label-width="70px"
       style="width: 400px; margin-left:50px;"
    >
      <el-form-item label="名称" prop="clientId">
        <el-input v-model="dataForm.clientId" placeholder="名称"></el-input>
      </el-form-item>

      <el-form-item label="密钥" prop="clientSecret">
        <el-input v-model="dataForm.clientSecret" placeholder="客户端密钥"></el-input>
      </el-form-item>
      <el-form-item label="token时间" prop="accessTokenValidity" label-width="80px">
        <el-input v-model="dataForm.accessTokenValidity" placeholder="token有效期"></el-input>
      </el-form-item>
      <el-form-item label="刷新Token" prop="refreshTokenValidity" label-width="82px">
        <el-input v-model="dataForm.refreshTokenValidity" placeholder="刷新Token有效期"></el-input>
      </el-form-item>
      <el-form-item label="区域" prop="area">
        <el-input v-model="dataForm.area" placeholder="系统部署所在区域"></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        clientId: "",
        accessTokenValidity: "",
        refreshTokenValidity: "",
        area: "",
        clientSecret:""
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/tenant/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(( data ) => {
            if (data && data.code === 0) {
              this.dataForm.clientId = data.sysTenant.clientId;
              this.dataForm.accessTokenValidity = data.sysTenant.accessTokenValidity;
              this.dataForm.refreshTokenValidity = data.sysTenant.refreshTokenValidity;
              this.dataForm.area = data.sysTenant.area;
               this.dataForm.clientSecret = data.sysTenant.clientSecret;
            }
          });
        }
      });
    },


        // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/tenant/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              clientId: this.dataForm.clientId,
              clientSecret: this.dataForm.clientSecret,
              area: this.dataForm.area,
              accessTokenValidity: this.dataForm.accessTokenValidity,
              refreshTokenValidity: this.dataForm.refreshTokenValidity
            }),
          }).then(( data ) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  },
};
</script>

<style lang="scss" scoped>
</style>